<?php
namespace app\service;

use app\model\UserCouponModel;

class UserCouponService
{
    protected $model;
    
    public function __construct()
    {
        $this->model = new UserCouponModel;
    }
    
    public function list($params)
    {
        $query = $this->model->newQuery();
        
        // 构建查询条件
                if (!empty($params['user_id'])) {
            $query->where('user_id', '=', str_replace('?', $params['user_id'], '?'));
        }
        if (!empty($params['coupon_id'])) {
            $query->where('coupon_id', '=', str_replace('?', $params['coupon_id'], '?'));
        }
        if (!empty($params['order_id'])) {
            $query->where('order_id', '=', str_replace('?', $params['order_id'], '?'));
        }
        if (!empty($params['status'])) {
            $query->where('status', '=', str_replace('?', $params['status'], '?'));
        }
        if (!empty($params['created_at'])) {
            $query->where('created_at', 'between', str_replace('?', $params['created_at'], '?,?'));
        }
        if (!empty($params['used_time'])) {
            $query->where('used_time', 'between', str_replace('?', $params['used_time'], '?,?'));
        }

        
        return $query->paginate($params['page_size'] ?? 15);
    }
    
    public function create($data)
    {
        return $this->model->create($data);
    }
    
    public function update($id, $data)
    {
        $model = $this->model->findOrFail($id);
        return $model->update($data);
    }
    
    public function delete($id)
    {
        return $this->model->destroy($id);
    }
    
    public function find($id)
    {
        return $this->model->findOrFail($id);
    }

    public function batchDelete($ids)
    {
        return $this->model->destroy($ids);
    }
} 